#!/bin/bash

#Please place all the published files under ./binary-source directory

VERSION=8.0.25
SRC_DIR=binary-source
SOURCE_NAME="src"

rm -rf "${SOURCE_NAME}"
rm -rf pkg
rm -rf src
rm -f "${SOURCE_NAME}*pkg.tar.*"
rm -f PKGBUILD

mkdir "${SOURCE_NAME}"
mkdir -p "${SOURCE_NAME}/opt/xdman"
mkdir -p "${SOURCE_NAME}/usr/bin"
mkdir -p "${SOURCE_NAME}/usr/share/applications"

cp -r "${SRC_DIR}/." "${SOURCE_NAME}/opt/xdman"

find "${SOURCE_NAME}/" -type d -print0 | xargs -0 chmod 0755
find "${SOURCE_NAME}/" -type f -print0 | xargs -0 chmod 0644

touch "${SOURCE_NAME}/opt/xdman/xdm-app.desktop"
touch "${SOURCE_NAME}/usr/bin/xdman"

cat <<EOF > "${SOURCE_NAME}/opt/xdman/xdm-app.desktop"
[Desktop Entry]
Version=1.0
Encoding=UTF-8
Exec=env GTK_USE_PORTAL=1 /opt/xdman/xdm-app %U
Type=Application
Terminal=false
Name=Xtreme Download Manager
Comment=Xtreme Download Manager
Categories=Network;
Icon=/opt/xdman/xdm-logo.svg
MimeType=application/xdm-app;x-scheme-handler/xdm-app;
StartupNotify=true
EOF

cat <<EOF > "${SOURCE_NAME}/usr/bin/xdman"
#!/bin/bash
export GTK_USE_PORTAL=1 
/opt/xdman/xdm-app \$@
EOF

cat <<EOF > "${SOURCE_NAME}/opt/xdman/source_pkg.txt"
xdman_gtk|.pkg.tar.zst;.pkg.tar.xz;.pkg.tar.gz;.pkg.tar.bz2
EOF

cp "${SOURCE_NAME}/opt/xdman/xdm-app.desktop" "${SOURCE_NAME}/usr/share/applications"

chmod 755 "${SOURCE_NAME}/usr/bin/xdman"
chmod 755 "${SOURCE_NAME}/opt/xdman/xdm-app"

#tar --create --file "${SOURCE_NAME}.tar.gz" -C "${SOURCE_NAME}" .

cat <<EOF > "PKGBUILD"
pkgname=xdman_gtk
pkgver=${VERSION}
pkgrel=1
pkgdesc="Open source download accelerator and video downloader."
arch=("x86_64")
url="https://github.com/subhra74/xdm"
license=('GPLv3')
#source=("${SOURCE_NAME}.tar.gz")
depends=('ffmpeg' 'gtk3>=3.22.0')
#sha1sums=('SKIP')

package() {
	cp -r * \$pkgdir
}
EOF

cat PKGBUILD

makepkg -d -f